const mongoose=require("mongoose");
const request = require('request');
var Order=mongoose.model("order");
var merchant=mongoose.model("merchant");
var client=mongoose.model("client");
var config = require('../../../core/server/config/config');

exports.loadHistoryOrders = function (req,res) {
    var merchant = req.session.merchant;
    Order.find({$or:[{status:1},{status:2}],merchant:merchant._id}).populate("merchant").populate("client")
        .exec( function(err,order){
            if(!err){
                res.json({order:order,msg:"SUCCESS"});
            }else{
                res.json({error:"findOrders"});
            }
        });
};

exports.loadNewOrders = function (req,res) {
    var merchant = req.session.merchant;
    if(merchant){
    Order.find({status:0,merchant:merchant._id}).populate("merchant").populate("client")
        .exec( function(err,order){
            if(!err){
                res.json({order:order,msg:"SUCCESS"});
            }else{
                res.json({error:"findOrders"});
            }
        });
    }else{
        res.json({error:"Login please!"});
    }
};

exports.newOrders = function (req,res) {
    var merchant = req.session.merchant;
    Order.find({status:0,merchant:merchant._id})
        .exec( function(err,order){
            if(!err){
                res.json({length:order.length,msg:"SUCCESS"});
            }else{
                res.json({error:"newOrders"});
            }
        });
};

exports.loadOrdersDetails = function (req,res) {
    var oid = req.params.oid;
    Order.find({_id:oid},{orderItem:1})
        .exec( function(err,orderItem){
            if(!err){
                res.json({orderItem:orderItem,msg:"SUCCESS"});
            }else{
                res.json({error:"findOrderItem"});
            }
        });
};

exports.auditOrders = function (req,res) {
    var oid = req.params.oid;
    var status = req.body.status;
    Order.findOne({_id:oid})
        .exec( function(err,order){
            order.status = status;
            order.save(function(err,order){
                if(!err){
                    res.json({order:order,msg:"SUCCESS"});
                }else{
                    res.json({error:"auditOrders"});
                }
            })
        });
};

exports.loadComplain = function (req,res) {
    var merchant = req.session.merchant;
    var result = "";

    request.get({url:config.complain+merchant._id.toString()}, function (err, response, body) {
            console.log(JSON.parse(body));
            result = JSON.parse(body);

        Order.find({complain:{$ne:null},merchant:merchant._id.toString()})
            .populate("merchant").populate("client")
            .exec( function(err,order){
                if(!err){

                    for(var i = 0;i < order.length; i++){
                        for(var key in result){
                            if(order[i]._id == key){
                                order[i].res = result[key];
                            }
                        }
                    }
                    res.json({order:order,msg:"SUCCESS"});
                }else{
                    res.json({error:"loadComplain"});
                }
            });

    });


};